home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / earcd / misc / emu / arosdev.lha / AROS / workbench / c / type.c < prev    next >
C/C++ Source or Header  |  1997-01-27  |  5KB  |  305 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: type.c,v 1.6 1997/01/27 00:22:38 ldp Exp $
  4.     $Log: type.c,v $
  5.     Revision 1.6  1997/01/27 00:22:38  ldp
  6.     Include proto instead of clib
  7.  
  8.     Revision 1.5  1996/09/17 16:43:01  digulla
  9.     Use general startup code
  10.  
  11.     Revision 1.4  1996/09/13 17:52:11  digulla
  12.     Use IPTR
  13.  
  14.     Revision 1.3  1996/08/13 15:34:04  digulla
  15.     #include <exec/execbase.h> was missing
  16.  
  17.     Revision 1.2  1996/08/01 17:40:46  digulla
  18.     Added standard header for all files
  19.  
  20.     Desc:
  21.     Lang:
  22. */
  23. #include <exec/memory.h>
  24. #include <exec/execbase.h>
  25. #include <proto/exec.h>
  26. #include <dos/dos.h>
  27. #include <proto/dos.h>
  28.  
  29. #define BUFSIZE 8192
  30.  
  31. struct file
  32. {
  33.     BPTR fd;
  34.     UBYTE *cur;
  35.     ULONG cnt;
  36.     LONG error;
  37.     UBYTE buf[BUFSIZE];
  38. };
  39.  
  40. const UBYTE hs[16]="0123456789abcdef";
  41.  
  42. #define putc(f,c) (*(f)->cur++=(c),--(f)->cnt?0:put(f))
  43. static int put(struct file *f)
  44. {
  45.     LONG size,subsize;
  46.     STRPTR buf;
  47.     size=f->cur-f->buf;
  48.     buf=f->buf;
  49.     while(size)
  50.     {
  51.     subsize=Write(f->fd,buf,size);
  52.     if(subsize<=0)
  53.     {
  54.         f->error=IoErr();
  55.         return 1;
  56.     }
  57.     buf+=subsize;
  58.     size-=subsize;
  59.     }
  60.     f->cur=f->buf;
  61.     f->cnt=BUFSIZE;
  62.     return 0;
  63. }
  64.  
  65. #define getc(f) ((f)->cnt--?*(f)->cur++:get(f))
  66. static int get(struct file *f)
  67. {
  68.     LONG size;
  69.     size=Read(f->fd,f->buf,BUFSIZE);
  70.     if(size<0)
  71.     f->error=IoErr();
  72.     if(size<=0)
  73.     return -1;
  74.     f->cnt=size-1;
  75.     f->cur=f->buf;
  76.     return *f->cur++;
  77. }
  78.  
  79. static void putlinequick(struct file *f, ULONG offset, UBYTE *buf)
  80. {
  81.     int c, i, k;
  82.     UBYTE *b, *o;
  83.     o=f->cur;
  84.     if(offset>=0x10000)
  85.     {
  86.     if(offset>=0x100000)
  87.     {
  88.         if(offset>=0x1000000)
  89.         {
  90.         if(offset>=0x10000000)
  91.         {
  92.             *o++=hs[(offset>>28)&0xf];
  93.             f->cnt--;
  94.         }
  95.         *o++=hs[(offset>>24)&0xf];
  96.         f->cnt--;
  97.         }
  98.         *o++=hs[(offset>>20)&0xf];
  99.         f->cnt--;
  100.     }
  101.     *o++=hs[(offset>>16)&0xf];
  102.     f->cnt--;
  103.     }
  104.     *o++=hs[(offset>>12)&0xf];
  105.     *o++=hs[(offset>>8)&0xf];
  106.     *o++=hs[(offset>>4)&0xf];
  107.     *o++=hs[offset&0xf];
  108.     *o++=':';
  109.     *o++=' ';
  110.     b=buf;
  111.     for(i=0;i<4;i++)
  112.     {
  113.     for(k=0;k<4;k++)
  114.     {
  115.         c=*b++;
  116.         *o++=hs[c>>4];
  117.         *o++=hs[c&0xf];
  118.     }
  119.     *o++=' ';
  120.     }
  121.     b=buf;
  122.     for(i=0;i<16;i++)
  123.     {
  124.     c=*b++;
  125.     *o++=(c&0x7f)>=0x20&&c!=0x7f?c:'.';
  126.     }
  127.     *o++='\n';
  128.     f->cur=o;
  129.     f->cnt-=59;
  130. }
  131.  
  132. static int putline(struct file *f, ULONG offset, UBYTE *buf, ULONG num)
  133. {
  134.     int c, i;
  135.     UBYTE *b;
  136.     if(offset>=0x10000)
  137.     {
  138.     if(offset>=0x10000000&&putc(f,hs[(offset>>28)&0xf]))
  139.         return 1;
  140.     if(offset>=0x1000000&&putc(f,hs[(offset>>24)&0xf]))
  141.         return 1;
  142.     if(offset>=0x100000&&putc(f,hs[(offset>>20)&0xf]))
  143.         return 1;
  144.     if(offset>=0x10000&&putc(f,hs[(offset>>16)&0xf]))
  145.         return 1;
  146.     }
  147.     if(putc(f,hs[(offset>>12)&0xf]))
  148.     return 1;
  149.     if(putc(f,hs[(offset>>8)&0xf]))
  150.     return 1;
  151.     if(putc(f,hs[(offset>>4)&0xf]))
  152.     return 1;
  153.     if(putc(f,hs[offset&0xf]))
  154.     return 1;
  155.     if(putc(f,':'))
  156.     return 1;
  157.     if(putc(f,' '))
  158.     return 1;
  159.     b=buf;
  160.     for(i=0;i<16;i++)
  161.     {
  162.     if(i<num)
  163.     {
  164.         c=*b++;
  165.         if(putc(f,hs[c>>4]))
  166.         return 1;
  167.         if(putc(f,hs[c&0xf]))
  168.         return 1;
  169.     }else
  170.     {
  171.         if(putc(f,' '))
  172.         return 1;
  173.         if(putc(f,' '))
  174.         return 1;
  175.     }
  176.     if((i&3)==3)
  177.         if(putc(f,' '))
  178.         return 1;
  179.     }
  180.     b=buf;
  181.     for(i=0;i<num;i++)
  182.     {
  183.     c=*b++;
  184.     if(putc(f,(c&0x7f)>=0x20&&c!=0x7f?c:'.'))
  185.         return 1;
  186.     }
  187.     if(putc(f,'\n'))
  188.     return 1;
  189.     return 0;
  190. }
  191.  
  192. void hexdumpfile(struct file *in, struct file *out)
  193. {
  194.     UBYTE buf[16];
  195.     UBYTE *b;
  196.     LONG offset=0, n, c, tty;
  197.     tty=IsInteractive(out->fd);
  198.     for(;;)
  199.     {
  200.     if(in->cnt>16)
  201.     {
  202.         b=in->cur;
  203.         n=16;
  204.         in->cur+=16;
  205.         in->cnt-=16;
  206.     }else
  207.     {
  208.         b=buf;
  209.         for(n=0;n<16;n++)
  210.         {
  211.         c=getc(in);
  212.         if(c<0)
  213.             break;
  214.         b[n]=c;
  215.         }
  216.     }
  217.     if(n==16)
  218.     {
  219.         if(out->cnt>=63)
  220.         putlinequick(out,offset,b);
  221.         else
  222.         if(putline(out,offset,b,n))
  223.             return;
  224.     }else
  225.     {
  226.         if(n)
  227.         putline(out,offset,b,n);
  228.         if(out->cur!=out->buf)
  229.         put(out);
  230.         return;
  231.     }
  232.     if(tty)
  233.         if(put(out))
  234.         return;
  235.     offset+=n;
  236.     }
  237. }
  238.  
  239. LONG dumpfile(struct file *in, struct file *out)
  240. {
  241.     LONG c;
  242.     if(1/*IsInteractive(out->fd)*/)
  243.     for(;;)
  244.     {
  245.         c=getc(in);
  246.         if(c<0)
  247.         return 0;
  248.         if(putc(out,c)||(c=='\n'&&put(out)))
  249.         return 1;
  250.     }
  251. }
  252.  
  253. int main (int argc, char ** argv)
  254. {
  255.     IPTR args[5]={ 0, 0, 0, 0, 0 };
  256.     struct RDArgs *rda;
  257.     struct file *in, *out;
  258.     STRPTR *names;
  259.  
  260.     rda=ReadArgs("FROM/A/M,TO/K,OPT/K,HEX/S,NUMBER/S",args,NULL);
  261.     if(rda==NULL)
  262.     {
  263.     PrintFault(IoErr(),"Type");
  264.     return RETURN_FAIL;
  265.     }
  266.     names=(STRPTR *)args[0];
  267.  
  268.     in =AllocMem(sizeof(struct file),MEMF_ANY);
  269.     out=AllocMem(sizeof(struct file),MEMF_ANY);
  270.  
  271.     if(in!=NULL&&out!=NULL)
  272.     {
  273.     out->cur=out->buf;
  274.     out->cnt=BUFSIZE;
  275.     out->fd=Output();
  276.     while(*names!=NULL)
  277.     {
  278.         in->fd=Open(*names,MODE_OLDFILE);
  279.         if(in->fd)
  280.         {
  281.         in->cnt=0;
  282.         if(args[3])
  283.             hexdumpfile(in,out);
  284.         else
  285.             dumpfile(in,out);
  286.         Close(in->fd);
  287.         }else
  288.         {
  289.         PrintFault(IoErr(),"Type");
  290.         break;
  291.         }
  292.         names++;
  293.     }
  294.     }else
  295.     PrintFault(ERROR_NO_FREE_STORE,"Type");
  296.  
  297.     if(in!=NULL)
  298.     FreeMem(in,sizeof(struct file));
  299.     if(out!=NULL)
  300.     FreeMem(out,sizeof(struct file));
  301.     if(rda!=NULL)
  302.     FreeArgs(rda);
  303.     return 0;
  304. }
  305.